package com.hm.health.provider;

import android.content.Context;
import android.util.Log;

import com.goodix.http.HttpConstants;
import com.goodix.http.WebServiceManager;
import com.goodix.http.jsonparams.CfgDetailInfo;
import com.goodix.http.jsonparams.DeviceInfo;
import com.goodix.http.jsonparams.response.QueryCfgInfoResponse;
import com.goodix.http.jsonparams.response.QueryDeviceInfoResponse;
import com.goodix.iot.libbase.logger.Glog;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * Created by chenshi on 2017/9/15.
 */
public class DataServiceManager {

    private static final String TAG = DataServiceManager.class.getSimpleName();

    private static DataServiceManager mInstance;

    private Context mContext;

    public DataServiceManager(Context context) {
        mContext = context;
    }

    public static synchronized DataServiceManager getInstance(Context context) {
        if (null == mInstance) {
            mInstance = new DataServiceManager(context);
        }
        return mInstance;
    }

    public void updateCfgData() {
        Glog.i(TAG, "updateCfgData");
        Observable.create(new Observable.OnSubscribe<QueryCfgInfoResponse>() {
            @Override
            public void call(Subscriber<? super QueryCfgInfoResponse> subscriber) {
                QueryCfgInfoResponse response = WebServiceManager.getInstance().getCfgInfo();
                subscriber.onNext(response);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread())
                .subscribe(new Observer<QueryCfgInfoResponse>() {
                    @Override
                    public void onNext(QueryCfgInfoResponse response) {
                        Log.i(TAG, "onNext------------------");
                        if (null != response && response.getStatus() == HttpConstants.RESULT_STATUS_SUCCESS) {
                            Log.i(TAG, "onNext------------------SUCCESS");
                            DbProvider.getInstance(mContext).open();
                            if (response.getSceneInfo() != null
                                    && response.getSceneInfo().getChild() != null
                                    && response.getSceneInfo().getChild().size() > 0) {
                                DbProvider.getInstance(mContext).removeAllSceneInfo();
                                for (CfgDetailInfo scene : response.getSceneInfo().getChild()) {
                                    DbProvider.getInstance(mContext).addSceneInfo(scene);
                                }
                            }
                            if (response.getSubjectInfo() != null
                                    && response.getSubjectInfo().getChild() != null
                                    && response.getSubjectInfo().getChild().size() > 0) {
                                DbProvider.getInstance(mContext).removeAllSubjectInfo();
                                for (CfgDetailInfo subject : response.getSubjectInfo().getChild()) {
                                    DbProvider.getInstance(mContext).addSubjectInfo(subject);
                                }
                            }
                            if (response.getSkinInfo() != null
                                    && response.getSkinInfo().getChild() != null
                                    && response.getSkinInfo().getChild().size() > 0) {
                                DbProvider.getInstance(mContext).removeAllSkinInfo();
                                for (CfgDetailInfo skin : response.getSkinInfo().getChild()) {
                                    DbProvider.getInstance(mContext).addSkinInfo(skin);
                                }
                            }
                            DbProvider.getInstance(mContext).close();
                            updateDeviceInfoData();
                        } else {
                            Log.e(TAG, "onNext------------------FAILED");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        Log.i(TAG, "onCompleted------------------");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError------------------" + e);
                    }
                });
    }

    private void updateDeviceInfoData() {
        Log.i(TAG, "updateDeviceInfoData");
        Observable.create(new Observable.OnSubscribe<QueryDeviceInfoResponse>() {
            @Override
            public void call(Subscriber<? super QueryDeviceInfoResponse> subscriber) {
                QueryDeviceInfoResponse response = WebServiceManager.getInstance().getDeviceInfo();
                subscriber.onNext(response);
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread())
                .subscribe(new Observer<QueryDeviceInfoResponse>() {
                    @Override
                    public void onNext(QueryDeviceInfoResponse response) {
                        Log.i(TAG, "onNext------------------");
                        if (null != response && response.getStatus() == HttpConstants.RESULT_STATUS_SUCCESS) {
                            Log.i(TAG, "onNext------------------SUCCESS");
                            if (response.getData() != null && response.getData().size() > 0) {
                                DbProvider.getInstance(mContext).open();
                                DbProvider.getInstance(mContext).removeAllDeviceInfo();
                                for (DeviceInfo device : response.getData()) {
                                    DbProvider.getInstance(mContext).addDeviceInfo(device);
                                }
                                DbProvider.getInstance(mContext).close();
                            }
                        } else {
                            Log.e(TAG, "onNext------------------FAILED");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        Log.i(TAG, "onCompleted------------------");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError------------------" + e);
                    }
                });
    }
}
